// stores/counter.js
import { defineStore } from "pinia";
import LocalCache from "@/libs/utils/cache";
import Router from "@/libs/router/index";
import { ElMessage } from "element-plus";

import {
  accountLoginRequest,
  accountRegisterRequest,
} from "@/libs/service/api/app/app";

/**
 * 根
 */

export const appPinia = defineStore("app", () => {
  // 登录逻辑
  async function loginActions(playLoad: any) {
    const result: any = await accountLoginRequest({
      username: playLoad.account,
      password: playLoad.password,
    });

    if (result.code === 200) {
      ElMessage.success(result.msg);
      LocalCache.setCache("token", result.token);
      setTimeout(() => {
        Router.push("/main");
      });
    } else {
      ElMessage.error(result.error);
    }
  }
  async function loginoutActions() {
    LocalCache.deleteCache("token");
  }

  return {
    loginActions,
    loginoutActions,
  };
});
